<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('dzd.quick Search Fields') })"
        >
        </TableHeader>

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" >
            <template #columnAppend>
                <el-table-column label="操作" width="180" >
                    <template #default="scope">
                        <el-button type="primary" size="small" v-if="menu.type==0&&scope.row.FPSQ=='0'" @click="handleExamine(scope.row)">开票申请</el-button>
                        <el-button type="primary" size="small" v-if="menu.type==1" @click="handleExamine(scope.row)">{{ scope.row.CHECKED == '0'?'审批':'取消审批' }}</el-button>
                    </template>
                </el-table-column>
            </template>
        </Table>
        <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm()"
        >
            <template #header>
                <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                    {{menu.label}}对账单操作
                </div>
            </template>
            
        </el-dialog>
        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { dzd } from '/@/api/controllerUrls'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'


const { t } = useI18n()

const route = useRoute()
const menu = reactive({
    type: route.name == 'dzdApprove/shou'?0:1,
    label: route.name == 'dzdApprove/shou'?'应收':'应付',
})
provide('menu',menu)


const tableRef = ref()
const optButtons = defaultOptButtons(["edit","delete"])
const baTable = new baTableClass(
    new baTableApi(dzd,'_invoicing'),
    {
        pk: 'ID',
        column: [
            { type: 'selection', align: 'center', operator: false },
            // { label: t('dzd.ID'), prop: 'ID', align: 'center', operator: 'RANGE' },
            { label: t('dzd.DZDNO'), prop: 'DZDNO', align: 'center' },
            // { label: t('dzd.JOBNO'), prop: 'JOBNO', align: 'center' },
            // { label: t('dzd.BILLTYPE'), prop: 'BILLTYPE', align: 'center', render: 'tag', replaceValue: { 1: t('dzd.BILLTYPE 1') } },
            { label: t('dzd.FPSQ'), prop: 'FPSQ', align: 'center',render:'tag',replaceValue:{'0':'待申请','1':"已申请"},show:menu.label=='应收' },
            { label: t('dzd.CORP'), prop: 'CORP', align: 'center' },
            { label: t('dzd.AMEND'), prop: 'AMEND', align: 'center' },
            { label: t('dzd.DZOP'), prop: 'DZOP', align: 'center' },
            { label: t('dzd.DZDATE'), prop: 'DZDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            // { label: t('dzd.JOBOP'), prop: 'JOBOP', align: 'center' },
            // { label: t('dzd.JOBDATE'), prop: 'JOBDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            { label: t('dzd.NOTE'), prop: 'NOTE', align: 'center',operator:false },
            // { label: t('dzd.FPSQR'), prop: 'FPSQR', align: 'center' },
            // { label: t('dzd.FPSQDATE'), prop: 'FPSQDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            // { label: t('dzd.CHECKED'), prop: 'CHECKED', align: 'center' },
            // { label: t('dzd.CHECKER'), prop: 'CHECKER', align: 'center' },
            // { label: t('dzd.CHECKDATE'), prop: 'CHECKDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },

            // { label: t('operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined, ],
        defaultOrder: { prop: 'ID', order: 'desc' },
        showComSearch:true
    },
    {
        defaultItems: [],
    },
    {
        onTableDblclick:(table:any)=>{
            baTable.form.operate = 'view'
            baTable.form.items = table.row
            baTable.form.extend = {
                ...menu
            }
            return false
        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.table.filter!.type = menu.type 
    
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

const handleExamine = (item:any)=>{
    createAxios({
        url:dzd + 'examine',
        method:'get',
        params:{
            type:menu.type,
            id:item.ID,
            checked:menu.type==0?undefined:item.CHECKED=='0'?'1':'0'
        }
    }).then(()=>{
        let message = ''
        if(menu.type==0) message = '申请成功！'
        if(menu.type==1 && item.CHECKED=='0') message = '审批成功！'
        if(menu.type==1 && item.CHECKED=='1') message = '取消成功！'
        ElMessage({
            message,
            type:'success'
        })
        baTable.getIndex()
        
    })
    console.log(item)
}


</script>

<script lang="ts">
import { defineComponent } from 'vue'
import { useRoute } from 'vue-router'
import createAxios from '/@/utils/axios'
import { ElMessage } from 'element-plus'
export default defineComponent({
    name: 'dzd',
})
</script>

<style scoped lang="scss"></style>
